﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using RanchBuddy.Core.DataAccess.Impl;
using StructureMap;

namespace RanchBuddy.Core.Services.Impl
{
    [Pluggable("Default")]
    public class EmailService : IEmailService
    {
        public static void SendContactUsEmail(string Email, string Subject, string FirstName, string LastName, string Body, string Phone)
        {
            MailMessage mm = new MailMessage(ConfigurationService.GetFromEmailAddress(), "asiemer@hotmail.com");
            mm.Subject = "Website Contact - " + Subject;
            mm.Body = "Created and sent on " + DateTime.Now + "<BR>";
            mm.Body += "email: " + Email + "<BR>";
            mm.Body += "name: " + FirstName + " " + LastName + "<BR>";
            mm.Body += "phone: " + Phone + "<BR>";
            mm.Body += "<hr>" + Body;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public static void SendPasswordReminderEmail(string To, string Password)
        {
            MailMessage mm = new MailMessage(ConfigurationService.GetFromEmailAddress(), To);
            mm.Subject = HelpMessageService.GetHelpMessageBodyByID(37)
                .Replace("{SiteName}",ConfigurationService.GetSiteName());
            mm.Body = HelpMessageRepository.GetHelpMessageBodyByID(38)
                .Replace("{Password}", Password);
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public static void SendRegistrationEmail(string To, Guid Key)
        {
            MailMessage mm = new MailMessage(ConfigurationService.GetFromEmailAddress(), To);
            mm.Subject = HelpMessageService.GetHelpMessageBodyByID(11);
            mm.Body = HelpMessageService.GetHelpMessageBodyByID(12)
                .Replace("{key}", Key.ToString())
                .Replace("{root}", ConfigurationService.GetRoot());
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public static void SendEmail(string To, string CC, string BCC, string Subject, string Message)
        {
            MailMessage mm = new MailMessage(ConfigurationService.GetFromEmailAddress(),To);

            if(!string.IsNullOrEmpty(CC))
                mm.CC.Add(CC);

            if(!string.IsNullOrEmpty(BCC))
                mm.Bcc.Add(BCC);

            mm.Subject = Subject;
            mm.Body = Message;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        private static void Send(MailMessage Message)
        {
            Message.Subject = ConfigurationService.GetSiteName() + " - " + Message.Subject;
            SmtpClient smtp = new SmtpClient();
            smtp.Send(Message);
        }
    }
}